package org.lewis.woman.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.DbUtils;
import org.lewis.common.domain.User;
import org.lewis.common.jdbc.QueryHelper;
import org.lewis.common.service.UserService;
import org.lewis.common.utils.UserPwdUtils;

import com.tocersoft.framework.core.annotations.Component;

@Component("userService")
public class UserServiceImpl implements UserService {

	private final static String CREATEUSER = "insert into ts_user (email,name,password,createTime,status) VALUES (?,?,?,?,?)";

	private final static String GETUSERBYID = "select id,email,name,password,createTime,status,photo,mydesc,tags from ts_user where id=? ";

	private final static String GETUSERBYEMAIL = "select id,email,name,password,createTime,status, photo,mydesc,tags from ts_user where email=? ";
	
	private final static String GETUSERBYNAME = "select id,email,name,password,createTime,status, photo,mydesc,tags from ts_user where name=? ";

	private final static String GETUSERBYEMAILANDPWD = "select id,email,name,password,createTime,status,photo,mydesc,tags from ts_user where email=? and password=? ";

	private final static String UPDATEUSERSTATUS = "update ts_user set status=? where id=?";
	
	public long createUser(User user) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = QueryHelper.getConnection().prepareStatement(CREATEUSER,
					PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setObject(1, user.getEmail());
			ps.setObject(2, user.getName());
			ps.setObject(3, UserPwdUtils.getEncryptedPwd(user.getPassword()));
			ps.setObject(4, user.getCreateTime());
			ps.setObject(5, user.getStatus());
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
			return rs.next() ? rs.getLong(1) : -1;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(rs);
			DbUtils.closeQuietly(ps);
		}
		return 0;
	}

	public User getUserByEmail(String email) {
		return QueryHelper.get(User.class, GETUSERBYEMAIL, email);
	}

	public User getUserByEmailAndPwd(String email, String pwd) {
		return QueryHelper.get(User.class, GETUSERBYEMAILANDPWD, email, UserPwdUtils.getEncryptedPwd(pwd));
	}

	public User getUserById(long id) {
		return QueryHelper.get(User.class, GETUSERBYID, id);
	}

	public void updateUserStatus(long uid, int status) {
		QueryHelper.update(UPDATEUSERSTATUS,status,uid);
	}

	public User getUserByName(String name) {
		return QueryHelper.get(User.class, GETUSERBYNAME, name);
	}

	public List<User> queryByPage(int page, int size, Map<String, Object> data) {
		StringBuilder sqlBuilder=new StringBuilder();
		sqlBuilder.append("select id,email,name,password,createTime,status,photo,mydesc,tags from ts_user where 1=1 ");
		List<Object> parms=new ArrayList<Object>();
		if(data.get("name")!=null&&!"".equals(data.get("name"))){
			sqlBuilder.append(" and name like ?");
			parms.add("%"+data.get("name")+"%");
		}
		if(data.get("email")!=null&&!"".equals(data.get("email"))){
			sqlBuilder.append(" and email like ?");
			parms.add("%"+data.get("email")+"%");
		}
		if(data.get("stime")!=null&&data.get("etime")!=null&&!"".equals(data.get("stime"))&&!"".equals(data.get("etime"))){
			sqlBuilder.append(" and createTime between ? and ? ");
			parms.add(data.get("stime")+" 00:00:00 ");
			parms.add(data.get("etime")+" 23:59:59 ");
		}
		sqlBuilder.append(" order by createTime desc ");
		return QueryHelper.queryPage(User.class, sqlBuilder.toString(), page, size,parms.toArray());
	}

	public void updateUserPassword(long uid, String pwd) {
		String sql="update ts_user set password=? where id=?";
		QueryHelper.update(sql, UserPwdUtils.getEncryptedPwd(pwd),uid);
	}

	public void updateUserDesc(long uid, String desc) {
		String sql="update ts_user set mydesc=? where id=?";
		QueryHelper.update(sql, desc,uid);
	}

	public void updateUserTags(long uid, String tags) {
		String sql="update ts_user set tags=? where id=?";
		QueryHelper.update(sql, tags,uid);
	}

	public void updateUserPhoto(long uid, String photo) {
		String sql="update ts_user set photo=? where id=?";
		QueryHelper.update(sql, photo,uid);
	}

}
